#pragma once

#include <QTcpSocket>
#include "Protocol.h"

// 客户端
class Client: public QTcpSocket {
    Q_OBJECT

    signals: void UM_CHAT_S2C(ChatInfo*);                       // 聊天事件（服务器到客户端）
    signals: void UM_FRIEND_LOGOFF(LogoffRequest*);             // 好友离线事件
    signals: void UM_FRIEND_LOGON(FriendLogon*);                // 好友登录事件
    signals: void UM_GET_ADD_FRIEND_REQUEST(AddFriendRequest*); // 得到添加好友请求事件
    signals: void UM_GET_ADD_FRIEND_RESULT(AddFriendResult*);   // 得到添加好友结果事件
    signals: void UM_GET_LOGON_RESULT(LogonResult*);            // 得到登录结果事件
    signals: void UM_GET_REG_RESULT(RegResult*);                // 得到注册结果事件

    private: int mFriendCount;          // 好友数
    private: int mIconIndex;            // 头像索引
    private: static QString sUsername;  // 用户名

    public: explicit Client();
    public: ~Client();
    public: ResultCode addFriend(const char*);
    public: void allowAddFriend(bool, AddFriendRequest*);
    public: void chat(ChatInfo*);
    public: int getFriendCount();
    public: static const char* getUsername();
    private: void handleAddFriendRequest(AddFriendRequest*);
    private: void handleAddFriendResult(AddFriendResult*);
    private: void handleChat(ChatInfo*);
    private: void handleFriendLogoff(LogoffRequest*);
    private: void handleFriendLogon(FriendLogon*);
    private: void handleLogonResult(LogonResult*);
    private: void handleRegResult(RegResult*);
    public: void logoff();
    public: void logon(const char*, const char*);
    private: void onGetMessageFromServer(ImProtocol*);
    private slots: void onReceive();
    public: void reg(const char*, const char*, int);
    private: void sendData(ImProtocol*, qint64);
};
